
(function() {
  // const http = require('http');
  // const url = require('url');
  const fs = require('fs');
  const axios = require('axios');
  const cheerio = require('cheerio');
  // 为给定 ID 的 user 创建请求
  axios
    .post(
      'https://apinew.juejin.im/recommend_api/v1/article/recommend_all_feed',
      {
        id_type: 2,
        client_type: 2608,
        sort_type: 200,
        cursor: '0',
        limit: 20
      }
    )
    .then(async({ data: { data } }) => {
      if (data.length === 0) {
        console.log('没数据' + data);
        return false;
      }
      for (let i = 0; i < data.length; i++) {
        const res = await axios.get(
          data[i].item_info.url ||
            ('https://juejin.im/post/' +
              (data[i].item_info.article_id || data[i].item_info.advert_id))
        );
        const $ = cheerio.load(res.data, {
          normalizeWhitespace: false,
          xmlMode: false,
          decodeEntities: false
        });
        fs.appendFile(
          './spider/result.txt',
          $('article').html(),
          'utf8',
          err => {
            if (err) throw err;
            console.log('数据已被追加到文件');
          }
        );
      }
    })
    .catch(error => {
      console.log(error);
    });
})();
